<?php
/**
 * 商家
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author     NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

class merchantControl extends BaseHomeControl
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 商家的订单
     */
    public function orderOp()
    {
        /* @var orderModel $order_model */
        $order_model = Model('order');
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');

        $condition = array('merchant_id'=>$_SESSION['member']['merchant_id']);
        $state = isset($_GET['state']) ? intval($_GET['state']) : 0;
        switch ($state) {
            case 1:
                $condition['order_status'] = 10;
                break;
            case 2:
                $condition['order_status'] = 20;
                break;
            case 3:
                $condition['order_status'] = 21;
                break;
            case 4:
                $condition['order_status'] = 30;
                break;
        }
        $orders = $order_model->getOrderListPage($condition, '*', 10);
        foreach ($orders as $k => $order) {

            switch($order['order_type']){
                case 1:
                    $orders[$k]['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],HOTEL_MARK);
                    $orders[$k]['sku'] = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                    break;
                case 2:
                    $orders[$k]['ticket_thumb'] = UPLOAD.'ticket/'.$order['ticket_thumb'];
                    $orders[$k]['sku'] = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                    break;
                case 3:
                    $orders[$k]['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],FOOD_MARK);
                    $orders[$k]['sku'] =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
                case 4:
                    $orders[$k]['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],ITINERARY_MARK);
                    $orders[$k]['sku'] =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                    break;
            }
        }
        Tpl::output('_title','商家订单');
        Tpl::output('orders', $orders);
        Tpl::output('status', $order_model->status);
        Tpl::output('order_type',$order_model->type);

        //AJAX加载分页
        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {
            Tpl::showpage('merchant.order.page', 'null_layout');
            exit;
        }

        Tpl::output('TotalNum', pagecmd("gettotalnum"));
        Tpl::output('TotalPage', pagecmd("gettotalpage"));

        Tpl::output('state', $state);
        Tpl::showpage('merchant.order');
    }

    /**
     * 订单详情
     */
    public function orderdetailOp()
    {
        /* @var orderModel $order_model */
        $order_model = Model('order');
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');

        $ordersn = $_GET['ordersn'];
        $order = $order_model->getOrderInfo(array(
            'order_sn'=>$ordersn,
            'merchant_id'=>$_SESSION['member']['merchant_id'],
        ));
        switch($order['order_type']){
            case 1:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],HOTEL_MARK);
                $skus = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                break;
            case 2:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],TICKET_MARK);
                $skus = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                break;
            case 3:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],FOOD_MARK);
                $skus =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                break;
            case 4:
                $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],ITINERARY_MARK);
                $skus =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                break;
        }
        Tpl::output('order', $order);
        Tpl::output('skus', $skus);
        Tpl::output('_title','订单详情');
        Tpl::showpage('merchant.orderdetail');
    }

    /**
     * 核销订单
     */
    public function consumeOp()
    {
        /* @var orderModel $order_model */
        $order_model = Model('order');
        /* @var order_ticketModel $order_ticket_model */
        $order_ticket_model = Model('order_ticket');
        $code = isset($_GET['code']) ? $_GET['code'] : '';
        Tpl::output('_title','订单核销');
        if ($code) {
            Tpl::output('code', $code);
            //普通票圈
            $order = $order_model->getOrderInfo(array(
                'ticket_code'=>trim($code),
                'order_status'=>20,
                'merchant_id'=>$_SESSION['member']['merchant_id'],
            ));

            if ($order) {
                switch($order['order_type']){
                    case 1:
                        $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],HOTEL_MARK);
                        $order['sku'] = Model('order_hotel')->getOrderHotelList(array('order_id'=>$order['order_id']));
                        break;
                    case 2:
                        $order['ticket_thumb'] = UPLOAD . 'product/' . $order['ticket_thumb'];
                        $order['sku'] = $order_ticket_model->getOrderTicketList(array('order_id'=>$order['order_id']));
                        break;
                    case 3:
                        $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],FOOD_MARK);
                        $order['sku'] =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                        break;
                    case 4:
                        $order['ticket_thumb'] = getGoodsImg($order['ticket_thumb'],ITINERARY_MARK);
                        $order['sku'] =  Model('order_goods')->getOrderGoodsList(array('order_id'=>$order['order_id']));
                        break;
                }
                Tpl::output('order', $order);
                Tpl::showpage('merchant.consume');
                exit;
            }
        }
        Tpl::showpage('merchant.consume');
    }

    /**
     * 核销操作
     */
    public function doconsumeOp()
    {
        $order_sn = $_POST['order_sn'];

        if (isset($_GET['type']) && $_GET['type'] == 'act') {
            /** @var bargain_orderModel $order_model */
            $order_model = Model('bargain_order');
            $order = $order_model->getBgOrderInfo(array(
                'bg_order_status' => 20,
                'bg_order_sn' => $order_sn,
                'merchant_id' => $_SESSION['member']['merchant_id'],
            ));
            if ($order) {
                $res = $order_model->updateBgOrder(array('bg_order_id'=>$order['bg_order_id']), array('bg_order_status'=>21));
            }
        } else {
            /* @var orderModel $order_model */
            $order_model = Model('order');
            $order = $order_model->getOrderInfo(array(
                'order_status'=>20,
                'order_sn'=>$order_sn,
                'merchant_id'=>$_SESSION['member']['merchant_id'],
            ));
            //如果是酒店订单判断是否是后台确认过
            if($order['order_type']==1 && $order['if_examine']!=1){
                exit(json_encode(array('errcode'=>4000,'errmsg'=>'该订单后台还未审核,请您先审核再核销')));
            }
            if ($order) {
                $res = $order_model->updateOrder(array('order_id'=>$order['order_id']), array('order_status'=>21,'used_time'=>time()));
                //增加商家的钱包金额
                $merchant_model = Model('member_merchant');
                $res_2 =$merchant_model->addMerchantMoney($order['merchant_id'],'all_money',$order['need_pay']);
                //添加商家金额记录
                $pd_log_model = Model('pd_log');
                $pd_log_model->addPd_log(array(
                    'merchant_id'=>$order['merchant_id'],
                    'lg_member_name'=>$_SESSION['member']['mobile'],
                    'lg_type'=>1,//收入
                    'lg_av_amount'=>$order['need_pay'],
                    'lg_add_time'=>time(),
                    'lg_desc'=>'订单'.'['.$order['order_sn'] .']'.'核销成功,您收入'.$order['need_pay'].'元',
                    'order_sn'=>$order['order_sn'],
                    'order_name'=>$order['ticket_title'],
                ));
            }
        }

        if (isset($res) && $res && isset($res_2) && $res_2) {
            exit(json_encode(array('errcode'=>0,'errmsg'=>'核销成功')));
        }
        exit(json_encode(array('errcode'=>4000,'errmsg'=>'核销失败')));
    }

    /**
     * 商家活动订单管理
     */
    public function activityOp()
    {
        /** @var bargain_orderModel $model */
        $model = Model('bargain_order');

        $condition = array();
        if (isset($_GET['state']) && $_GET['state']) {
            $condition['bg_order_status'] = intval($_GET['state']);
        } else {
            $condition['bg_order_status'] = 20;
        }
        $condition['merchant_id'] = $_SESSION['member']['merchant_id'];

        $orders = $model->getBgOrderListPage($condition, '*', 10);
        foreach ($orders as $k => $order) {
            $orders[$k]['bg_thumb'] = UPLOAD.'product/'.$order['bg_thumb'];
        }
        Tpl::output('orders', $orders);

        //AJAX加载分页
        if (isset($_GET['isajax']) && $_GET['isajax'] == 1) {
            Tpl::showpage('merchant.act.page', 'null_layout');
            exit;
        }

        Tpl::output('TotalNum', pagecmd("gettotalnum"));
        Tpl::output('TotalPage', pagecmd("gettotalpage"));

        Tpl::output('state', $condition['bg_order_status']);
        Tpl::showpage('merchant.act');
    }

    /**
     * 活动订单详情
     */
    public function actdetailOp() {
        /** @var bargain_orderModel $model */
        $model = Model('bargain_order');

        $ordersn = $_GET['ordersn'];
        $order = $model->getBgOrderInfo(array(
            'bg_order_sn' => $ordersn,
            'merchant_id' => $_SESSION['member']['merchant_id'],
        ));
        $order['bg_thumb'] = UPLOAD.'product/'.$order['bg_thumb'];

        Tpl::output('order', $order);
        Tpl::showpage('merchant.actdetail');
    }

    /**
     * 我的钱包
     */
    public function  walletOp()
    {
        $merchant_model = Model('member_merchant');
        $info = $merchant_model->getMerchantInfo(array('merchant_id'=>$_SESSION['member']['merchant_id'],'is_disable'=>0));
        if(empty($info)){
            showMessage('非法操作','index.php?act=member');
        }
        if($_GET['type']==2){
            //查询提现列表
            $pd_cash_model = Model('pd_cash');
            $pd_list = $pd_cash_model->getPd_cashListPage(array('merchant_id'=>$_SESSION['member']['merchant_id']),'',10);
        }else{
            //收入
            $wallet_type = 1;
            $pd_log_model = Model('pd_log');
            $pd_list = $pd_log_model->getPd_logListPage(array('lg_type'=>$wallet_type,'merchant_id'=>$_SESSION['member']['merchant_id']),'',10);
        }
        Tpl::output('pd_list',$pd_list);
        //AJAX加载分页
        if(isset($_GET['isajax']) && $_GET['isajax'] == 1){
            Tpl::showpage('my.wallet.page','null_layout');
            exit;
        }
        Tpl::output('TotalNum',pagecmd("gettotalnum"));
        Tpl::output('TotalPage',pagecmd("gettotalpage"));

        Tpl::output('merchant_info',$info);
        Tpl::output('_title','我的钱包');
        Tpl::showpage('my_wallet','new_layout');
    }


    /**
     * 提现申请
     */
    public function pd_cashOp()
    {
        $merchant_model = Model('member_merchant');
        $info = $merchant_model->getMerchantInfo(array('merchant_id'=>$_SESSION['member']['merchant_id'],'is_disable'=>0));
        if(empty($info)){
            showMessage('非法操作','index.php?act=member');
        }
        Tpl::output('merchant_info',$info);
        if(chksubmit()){
            $pd_cash_model = Model('pd_cash');
            if($_POST['pdc_amount']>$info['all_money']){
                exit(json_encode(array('ecode'=>0,'msgs'=>'提现金额不能大于您的余额')));
            }
            $pd_cash = array(
                'pdc_sn'=>date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT),
                'merchant_id'=>$_SESSION['member']['merchant_id'],
                'member_name'=>$_SESSION['member']['mobile'],
                'pdc_amount'=>$_POST['pdc_amount'],
                'pdc_type'=>$_POST['pdc_type']=='ali'?2:1,
                'pdc_number'=>$_POST['pdc_number'],
                'pdc_user_name'=>$_POST['pdc_user_name'],
                'pdc_add_time'=>time(),
                'member_idcard'=>$_POST['member_idcard'],
                'real_amount'=>$info['pd_tariff']>0?money($_POST['pdc_amount'],$info['pd_tariff']):$_POST['pdc_amount'],
                'tariff'=>$info['pd_tariff'],
            );
            Model::beginTransaction();
            try{
                $res = $pd_cash_model->addPd_cash($pd_cash);
                //冻结钱包中余额
                if($res){
                    $merchant_model->reduceMerchantMoney($_SESSION['member']['merchant_id'],'all_money',$_POST['pdc_amount']);
                    $merchant_model->addMerchantMoney($_SESSION['member']['merchant_id'],'frozen_money',$_POST['pdc_amount']);
                    //增加支出记录
                    $pd_log_model = Model('pd_log');
                    $pd_log_model->addPd_log(array(
                        'merchant_id'=>$_SESSION['member']['member_id'],
                        'lg_member_name'=>$_SESSION['member']['mobile'],
                        'lg_type'=>2,
                        // 'lg_av_amount'=>$_POST['pdc_amount'],
                        'lg_freeze_amount'=>$_POST['pdc_amount'],
                        'lg_add_time'=>time(),
                        'lg_desc'=>'申请提现冻结金额'.$_POST['pdc_amount'].'提现单号'.'['.$pd_cash['pdc_sn'].']',
                        'order_sn'=>$pd_cash['pdc_sn'],
                        'order_name'=>'提现',
                    ));
                    Model::commit();
                    exit(json_encode(array('ecode'=>1,'msgs'=>'提现成功')));
                }else{
                    Model::rollback();
                    exit(json_encode(array('ecode'=>0,'msgs'=>'提现失败')));
                }
            }catch (\Exception $e){
                Model::rollback();
                exit(json_encode(array('ecode'=>0,'msgs'=>$e->getMessage())));
            }

        }
        if( $_GET['pd_type']=='ali' || $_GET['pd_type']=='wx'){
            Tpl::output('_title','申请提现');
            Tpl::showpage('pd_cash.form','new_layout');
        }else{
            Tpl::output('_title','申请提现');
            Tpl::showpage('pd_type','new_layout');
        }

    }

    /**
     * 提现详情
     */
    public function pd_cash_infoOp()
    {
        $pdc_id = intval($_GET['pdc_id']);
        if($pdc_id>0 || isset($_GET['sn'])){
            $pd_cash_model = Model('pd_cash');
            if($pdc_id>0){
                $pd_info = $pd_cash_model->getPd_cashInfo(array('pdc_id'=>$pdc_id));
            }else{
                $pd_info = $pd_cash_model->getPd_cashInfo(array('pdc_sn'=>$_GET['sn']));
            }
            Tpl::output('pd_info',$pd_info);
        }
        Tpl::output('_title','提现详情');
        Tpl::showpage('cash.info','new_layout');
    }


}
